﻿using CodeGenerator.ClassServices;
using CodeGenerator.Models;
using CodeGenerator.ProjectServices;
using CodeGenerator.Template;
using CodeGenerator.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeGenerator.Generators
{
    internal abstract class Generator : IGenerator
    {
        protected List<ClassModel> GetClassesByProjectId(int projectId)
        {
            //ทดสอบ
            //CodeGenBLClient client1 = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
            //ActionResultDtoArrayOfRelationshipModelDto result1 = client1.GetRelatipnshipModelBySourceClass(new ClassPkDto() { ClassId = 7 });
            //CodeGenBLClient client2 = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
            //ActionResultDtoArrayOfRelationshipModelDto result2 = client2.GetRelationshipModelByClassId(new ClassPkDto() { ClassId = 8 });
            //จบการทดสอบ

            CodeGenBLClient client = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
            ActionResultDtoArrayOfClassModelDto result = client.GetClassByProjectId(new ClassServices.ProjectPkDto { ProjectId = projectId });
            List<ClassModel> list = new List<ClassModel>();

            if (result.Status)
            {
                foreach (ClassModelDto dto in result.Data)
                {
                    ClassModel c = new ClassModel(dto);
                    c.SetAttributes(GetAttributes(c.Id));
                    list.Add(c);
                }

                list = GetParentClasses(list);
                list = GetChildrenClass(list);
                list = TransformFormula(list);  //Calculation Field


                //Start Add for Summary Field
                list = GetCalculationAttribute(list);
                // End Add for Summary Field
                return list;
            }
            else
                return null;
        }

        private List<AttributeModelDto> GetAttributes(int class_id)
        {
            CodeGenBLClient client = new CodeGenBLClient();
            ActionResultDtoArrayOfAttributeModelDto result = client.GetAttributesByClassId(new ClassPkDto() { ClassId = class_id });
            if (result.Status && result.Data != null)
            {
                return result.Data;
            }
            return new List<AttributeModelDto>();
        }

        // Start Add for Summary Field
        private List<ClassModel> GetCalculationAttribute(List<ClassModel> list)
        {
            List<AttributeModel> attributeModelList;
            string[] split = { "$", ".", " " };
            string[] obj;//= word.Split(split, StringSplitOptions.RemoveEmptyEntries);
            foreach (var classModel in list)
            {
                // Get all classmodel's attributes that it's data type is summary or calculation
                attributeModelList = classModel.GetAttributes().FindAll(x => x.DataType.Name == "Summary-Sum" ||
                                                x.DataType.Name == "Summary-Count" ||
                                                x.DataType.Name == "Summary-Min" ||
                                                x.DataType.Name == "Summary-Max");

                if (attributeModelList != null && attributeModelList.Count() != 0)
                {
                    foreach (var attr in classModel.GetAttributes().FindAll(x => x.DataType.Name == "Summary-Sum" ||
                                               x.DataType.Name == "Summary-Count" ||
                                               x.DataType.Name == "Summary-Min" ||
                                               x.DataType.Name == "Summary-Max"))
                    {
                        // Extract class model id and attribute model id (in case of data type is Summary family)
                        obj = attr.Formula.Split(split, StringSplitOptions.RemoveEmptyEntries);

                        //Get class by class id
                        var childClassModelForCalculate = GetClassByClassId(Convert.ToInt32(obj[0]));

                        //Assign calcultion attribute to class model's attribute for return 
                        attr.AttributeForSummary = childClassModelForCalculate.GetAttributes().Find(x => x.Id == Convert.ToInt32(obj[1]));

                        //Assign class model of calcultion attribute 
                        attr.AttributeForSummary.ClassModel = childClassModelForCalculate;

                    }

                }//end if list of datatype-specific attribute is null


            }// foreach class in list
            return list;
        }
        // End Add for Summary Field

        protected List<ClassModel> GetParentClasses(List<ClassModel> list)
        {
            foreach (ClassModel c in list)
            {
                c.ParentClass = new List<ClassModel>();

                CodeGenBLClient client = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
                ActionResultDtoArrayOfClassAndAttributeDto result = client.GetMasterDetailClassAndAttributeByDestinationClassId(new ClassPkDto() { ClassId = c.Id });

                if (result.Status && result.Data != null)
                {
                    foreach (ClassAndAttributeDto dto in result.Data)
                    {
                        ClassModel c2 = FindClassByClassId(dto.ClassId, list);
                        c.ParentClass.Add(c2);

                        bool isFound = false;

                        foreach (AttributeModel a in c._attributes)
                        {
                            if (a.IsLookup && !isFound)
                            {
                                List<LookupModel> list2 = GetLookupModel(a);
                                if (list2.Count > 0)
                                    if (c2.Id == FindClassModelByAttributeId(list2[0].DestAttribute.Id, list).Id)
                                    {
                                        isFound = true;
                                        a.IsLinkToParent = true;
                                        a.ParentClass = c2;
                                    }
                            }
                        }
                    }
                }
            }

            return list;
        }

        protected ClassModel FindClassModelByAttributeId(int id, List<ClassModel> list)
        {
            foreach (ClassModel c in list)
            {
                foreach (AttributeModel a in c._attributes)
                {
                    if (a.Id == id)
                        return c;
                }
            }

            return null;
        }

        protected List<ClassModel> GetChildrenClass(List<ClassModel> list)
        {
            foreach (ClassModel c in list)
            {
                c.ChildrenClass = new List<ClassModel>();
                List<RelationshipModel> listAll = GetAllRelationshipModel(c.Id);

                foreach (RelationshipModel r in listAll)
                {
                    //Wee Edit 20140209
                    if (r.SourceAttribute.IsMasterDetail)
                    {
                        //if (r.DestAttribute == null || r.DestAttribute.Id == 0)
                        foreach (AttributeModel a in c._attributes)
                        {
                            if (a.Id == r.SourceAttribute.Id) //จะได้ ลูกมา
                            {
                                a.ChildClass = FindClassByClassId(r.DestClassId, list); // 
                                c.ChildrenClass.Add(a.ChildClass);
                                //a.LookupClass = FindClassByClassId(r.SourceClassId, list);
                            }
                        }
                    }

                    if (r.SourceAttribute.IsLookup || r.SourceAttribute.IsMasterDetail) // หาว่าไป Lookup Class ไหนบ้าง
                    {
                        foreach (AttributeModel a in c._attributes)
                        {
                            if (a.Id == r.SourceAttribute.Id)
                            {
                                a.LookupClass = FindClassByClassId(r.DestClassId, list); // Class ที่ไป Lookup ตัวไหน เกิดจาก SourceAttribute 17
                            }
                            else if (a.Id == r.DestAttribute.Id)
                            {
                                a.LookupClass = FindClassByClassId(r.SourceClassId, list); // Class ที่ไป Lookup ตัวไหน เกิดจาก SourceAttribute 18
                            }
                        }
                    }

                }
            }

            return list;
        }

        protected ClassModel FindClassByClassId(int classId, List<ClassModel> list)
        {
            foreach (ClassModel c in list)
            {
                if (c.Id == classId)
                    return c;
            }
            return null;
        }

        protected ProjectModel GetProjectByProjectId(int projectId)
        {
            ProjectBLClient client = new ProjectBLClient();
            ActionResultDtoProjectDto result = client.GetProject(new ProjectServices.ProjectPkDto { ProjectId = projectId });

            if (result.Status)
            {
                ProjectModel p = new ProjectModel(result.Data);
                return p;
            }
            else
                return null;
        }

        protected ClassModel GetClassByClassId(int classId)
        {
            CodeGenBLClient client = new CodeGenBLClient();
            ActionResultDtoClassModelDto result = client.GetClass(new ClassPkDto { ClassId = classId });

            if (result.Status)
            {
                ClassModel c = new ClassModel(result.Data);
                return c;
            }
            else
                return null;
        }

        protected List<LookupClassModel> GetLookupClass(List<ClassModel> list)
        {
            List<LookupClassModel> l = new List<LookupClassModel>();

            //Start add for Summary field
            if (list.Count != 0 && list != null)
            {// End add for Summary field
                foreach (ClassModel c in list)
                {
                    foreach (AttributeModel a in c._attributes)
                    {
                        if (a.IsLookup)
                        {
                            CodeGenBLClient client = new CodeGenBLClient();
                            ActionResultDtoClassModelDto result = client.GetLookupClass(new AttributePkDto { Id = a.Id });

                            if (result.Status)
                                l.Add(new LookupClassModel(result.Data, a.Id));
                        }
                    }
                }
                //Start add for Summary field
            } // end add for Summary field
            return l;
        }
        // Add add for Summary field
        public static ClassModel GetDestinationClassByAttribute(AttributeModel attribute)
        {
            ClassModel c = null;
            if (attribute.IsMasterDetail)
            {
                CodeGenBLClient client = new CodeGenBLClient();
                ActionResultDtoClassModelDto result = client.GetDestinationRelationshipClass(new AttributePkDto { Id = attribute.Id });

                if (result.Status && result.Data != null)
                    c = new ClassModel(result.Data);
            }
            return c;
        }
        // End add for Summary field


        //Weerachai 23-12-2013
        internal static List<LookupModel> GetLookupModel(AttributeModel attribute)
        {
            List<LookupModel> l = new List<LookupModel>();
            CodeGenBLClient client = new CodeGenBLClient();
            ActionResultDtoArrayOfLookupModelDto result = client.GetLookupModel(new AttributePkDto() { Id = attribute.Id });
            if (result.Status && result.Data != null)
                foreach (LookupModelDto dto in result.Data)
                    l.Add(new LookupModel(dto));
            return l;
        }

        //Weerachai 12-01-2014
        internal static List<LookupModel> GetLookupModelByDestClass(int ClassId)
        {
            List<LookupModel> l = new List<LookupModel>();
            CodeGenBLClient client = new CodeGenBLClient();
            ActionResultDtoArrayOfLookupModelDto result = client.GetLookupModelByDestinationClass(new ClassPkDto { ClassId = ClassId });
            if (result.Status && result.Data != null)
                foreach (LookupModelDto dto in result.Data)
                    l.Add(new LookupModel(dto));
            return l;

        }

        //Weerachai 27-11-2013
        protected ClassModel GetDestinationClass(AttributeModel relationShip)
        {
            ClassModel c = null;
            if (relationShip.IsMasterDetail)
            {
                CodeGenBLClient client = new CodeGenBLClient();
                ActionResultDtoClassModelDto result = client.GetDestinationRelationshipClass(new AttributePkDto { Id = relationShip.Id });

                if (result.Status && result.Data != null)
                    c = new ClassModel(result.Data);
            }
            return c;
        }

        //Weerachai 27-11-2013
        //internal List<ClassModel> GetSourceClass(ClassModel child)
        //{
        //    List<ClassModel> list = new List<ClassModel>();
        //    CodeGenBLClient client = new CodeGenBLClient();
        //    List<RelationshipModel> relationships = GetRelationshipByDestinationClassId(child.Id);

        //    foreach (RelationshipModel dto in relationships)
        //    {
        //        if (dto.DestAttribute == null || dto.DestAttribute.Id == 0)
        //        {
        //            ClassModel c = GetClassByClassId(dto.SourceClassId);
        //            list.Add(c);
        //        }
        //    }

        //    if (list.Count > 0)
        //        return list;
        //    else
        //        return null;

        //    //end edit weerachai 21-12-2013 
        //}

        internal static List<PickupListModel> GetPickupList(AttributeModel attribute)
        {
            List<PickupListModel> p = new List<PickupListModel>();

            CodeGenBLClient client = new CodeGenBLClient();
            ActionResultDtoArrayOfPickupListDto result = client.GetPickupList(new AttributePkDto() { Id = attribute.Id });

            if (result.Status)
                foreach (PickupListDto dto in result.Data)
                    p.Add(new PickupListModel(dto, attribute.Id, attribute.Name));

            return p;
        }


        //Wee- 20140210 Get All Relationship
        protected List<RelationshipModel> GetAllRelationshipModel(int classId)
        {
            CodeGenBLClient client = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
            ActionResultDtoArrayOfRelationshipModelDto result = client.GetRelationshipModelByClassId(new ClassPkDto() { ClassId = classId });
            List<RelationshipModel> list = new List<RelationshipModel>();

            if (result.Status)
            {
                if (result.Data != null)
                    foreach (RelationshipModelDto dto in result.Data)
                    {
                        list.Add(new RelationshipModel(dto));
                    }
            }

            return list;
        }


        ////Get parent class
        //protected List<RelationshipModel> GetRelationshipByDestinationClassId(int DestClassId)
        //{
        //    CodeGenBLClient client = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
        //    ActionResultDtoArrayOfRelationshipModelDto result = client.GetRelationshipModelByDestinationClass(new ClassPkDto() { ClassId = DestClassId });
        //    List<RelationshipModel> list = new List<RelationshipModel>();

        //    if (result.Status)
        //    {
        //        if (result.Data != null)
        //            foreach (RelationshipModelDto dto in result.Data)
        //            {
        //                list.Add(new RelationshipModel(dto));
        //            }
        //    }

        //    return list;
        //}

        ////Get Child Class
        //protected List<RelationshipModel> GetRelationshipBySourceClassId(int SourceClassId)
        //{
        //    CodeGenBLClient client = new CodeGenBLClient("BasicHttpBinding_ICodeGenBL");
        //    ActionResultDtoArrayOfRelationshipModelDto result = client.GetRelationshipModelBySourceClass(new ClassPkDto() { ClassId = SourceClassId });
        //    List<RelationshipModel> list = new List<RelationshipModel>();

        //    if (result.Status)
        //    {
        //        if (result.Data != null)
        //            foreach (RelationshipModelDto dto in result.Data)
        //            {
        //                list.Add(new RelationshipModel(dto));
        //            }
        //    }

        //    return list;
        //}

        protected LookupClassModel FindLookupClass(List<LookupClassModel> list, int attributeId)
        {
            foreach (LookupClassModel l in list)
            {
                if (l.ForAttributeId.Equals(attributeId))
                    return l;
            }
            return new LookupClassModel();
        }

        protected void GenerateFiles(MetaData data, List<LookupClassModel> lookups, string packageName, string outputPath)
        {
            //string packagePath = packageName.Replace(".", "/");
            //List<string> ttFiles = Directory.GetFiles("Template/" + packagePath + "/", "*.tt", SearchOption.TopDirectoryOnly).ToList<string>();

            string packagePath = packageName.Replace(".", "/");
            string exPath = @"C:\test\TT";
            List<string> ttFiles;

            if (Directory.Exists("Template/" + packagePath + "/"))
            {
                ttFiles = Directory.GetFiles("Template/" + packagePath + "/", "*.tt", SearchOption.TopDirectoryOnly).ToList<string>();
            }
            else
            {
                ttFiles = Directory.GetFiles(exPath + "/Template/" + packagePath + "/", "*.tt", SearchOption.TopDirectoryOnly).ToList<string>();
            }

            //bool checkPreselected = true;
            foreach (string filepath in ttFiles)
            {
                string filename = Path.GetFileNameWithoutExtension(filepath);
                for (int i = 0; i < data.Models.Count; i++)
                {
                    ClassModel modelItem = data.Models[i];

                    ITemplate dto = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template." + packageName + "." + filename));

                    bool skip = false;

                    if (!skip) //if skip เป็น true
                    {
                        if (dto.IsRequiredOnlyOneMember)
                            dto.Data = new MetaData() { Project = data.Project, Models = new List<ClassModel>() { modelItem } };
                        else
                            dto.Data = data;

                        dto.lookups = lookups;

                        string dtoPath = outputPath + @"/{ProjectName}." + packagePath + "/";

                        if (dto.IsGenerateFolder)
                            dtoPath = outputPath + @"/{ProjectName}." + packagePath + "/{ClassName}/";

                        //if (dto.Data.Project.PreselectedSearch && filename.StartsWith("Preselected") && checkPreselected)
                        //{
                        //    string destinationPath = dtoPath.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                        //    DirectoryExt.CreateIfMissing(destinationPath);
                        //    string filenameApplyFormat = dto.Filename.Replace("{ClassName}", modelItem.Name).Replace("{ProjectName}", data.Project.ProjectName);
                        //    GenerateFile(dto, filenameApplyFormat, destinationPath);
                        //    checkPreselected = false;
                        //}

                        if (dto.GenerateStyle.Equals(GenerateStyle.Normal))
                        {
                            string destinationPath = dtoPath.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                            DirectoryExt.CreateIfMissing(destinationPath);
                            string filenameApplyFormat = dto.Filename.Replace("{ClassName}", modelItem.Name).Replace("{ProjectName}", data.Project.ProjectName);
                            GenerateFile(dto, filenameApplyFormat, destinationPath);
                        }

                        if (dto.GenerateStyle.Equals(GenerateStyle.LookUp))
                        {
                            string destinationPath = dtoPath.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", "Popup");
                            DirectoryExt.CreateIfMissing(destinationPath);
                            List<AttributeModel> attributes = modelItem._attributes.FindAll(a => a.IsLookup);
                            foreach (AttributeModel a in attributes)
                            {
                                List<LookupClassModel> list = new List<LookupClassModel>();
                                list.Add(this.FindLookupClass(lookups, a.Id));
                                //dto.lookups = new List<LookupClassModel>() { this.FindLookupClass(lookups, a.Id) };
                                dto.lookups = list;

                                /* apple
                                string filenameApplyFormat = dto.Filename.Replace("{ClassName}", a.Name).
                                                            Replace("{ProjectName}", data.Project.ProjectName);
                                GenerateFile(dto, filenameApplyFormat, destinationPath);
                            
                                */

                                /*apple*/
                                string filenameApplyFormat = dto.Filename.Replace("{AttributeName}", a.Name).
                                                            Replace("{ClassName}", modelItem.Name). //apple
                                                            Replace("{ProjectName}", data.Project.ProjectName);
                                GenerateFile(dto, filenameApplyFormat, destinationPath);
                                /*End Apple 12.11.2013 */

                                //New Instance สำหรับ วน Loop ให้ Attribute ถัดไป
                                dto = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template." + packageName + "." + filename));

                                if (dto.IsRequiredOnlyOneMember)
                                    dto.Data = new MetaData() { Project = data.Project, Models = new List<ClassModel>() { modelItem } };
                                else
                                    dto.Data = data;

                                dto.lookups = lookups;
                            }
                        }

                        if (dto.GenerateStyle.Equals(GenerateStyle.PickUp))
                        {
                            string destinationPath = dtoPath.Replace("{ProjectName}", data.Project.ProjectName).Replace("{ClassName}", modelItem.Name);
                            DirectoryExt.CreateIfMissing(destinationPath);
                            List<AttributeModel> attributes = modelItem._attributes.FindAll(a => a.DataType.Name.Equals("Pickup List"));
                            foreach (AttributeModel a in attributes)
                            {
                                dto.pickuplist = a.GetPickupList();

                                string filenameApplyFormat = dto.Filename.Replace("{ClassName}", a.Name).
                                                            Replace("{ProjectName}", data.Project.ProjectName).
                                                            Replace("{PicklistName}", a.Name);

                                GenerateFile(dto, filenameApplyFormat, destinationPath);

                                //New Instance สำหรับ วน Loop ให้ Attribute ถัดไป
                                dto = (ITemplate)Activator.CreateInstance(Type.GetType("CodeGenerator.Template." + packageName + "." + filename));

                                if (dto.IsRequiredOnlyOneMember)
                                    dto.Data = new MetaData() { Project = data.Project, Models = new List<ClassModel>() { modelItem } };
                                else
                                    dto.Data = data;
                            }
                        }

                        if (dto.IsGenerateOneTime)
                            i = data.Models.Count;
                    }
                }
            }
        }

        public void GenerateFile(ITemplate dto, string filenameApplyFormat, string destinationPath)
        {
            string outputString = dto.TransformText();
            string destinationFilePath = destinationPath + "/" + filenameApplyFormat;

            if (File.Exists(destinationFilePath))
                File.Delete(destinationFilePath);

            File.WriteAllText(destinationFilePath, outputString, Encoding.GetEncoding("UTF-8"));
        }

        public abstract void Generate(int projectId, string outputPath);

        //Calculation Field
        protected List<ClassModel> TransformFormula(List<ClassModel> list)
        {
            foreach (ClassModel clsmodel in list)
            {
                foreach (AttributeModel attmodel in clsmodel.GetAttributes())
                {
                    if (attmodel.FieldType.Name == "Calculation Field")
                    {
                        attmodel.TransFormula = GetTransformFormula(attmodel);
                    }
                }
            }
            return list;
        }

        //Calculation Field
        public string GetTransformFormula(AttributeModel input)
        {
            string oldFormula = input.Formula;
            string[] separators = null;
            string[] words = null;
            Dictionary<string, string> temp = new Dictionary<string, string>();
            if ((input.DataType.Name.Equals("String")) || (input.DataType.Name.Equals("DateTime")))
            {
                // concatenate(concat), substring (substr), **contain (cont)  
                separators = new string[] { ".concat", ".substr", "AddDate", "AddMonth", "AddYear", "Minimum","Summary", "Maximun", "[", "]", "DateDiff", ",", "(", ")" };
                words = input.Formula.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                foreach (string word in words)
                {
                    if (word.Contains('+') || word.Contains('-') || word.Contains('*') || word.Contains('/'))
                    {   //contain math operation go to math cal function
                        Dictionary<string, string> temp1 = MathFunction(word);
                        string sentence = word;

                        //build result for one key
                        foreach (var b in temp1)
                            sentence = sentence.Replace(b.Key, b.Value);
                        temp.Add(word, sentence);
                    }
                    else
                    {
                        string newWord = word.Replace('(', ' ');
                        newWord = newWord.Replace(')', ' ');
                        newWord = newWord.Trim();
                        string syntaxDB = "";
                        if (newWord.IndexOf('$') == 0)
                        {
                            string[] split = { "$", ".", " " };
                            string[] obj = newWord.Split(split, StringSplitOptions.RemoveEmptyEntries);

                            if (obj.Count() == 2)
                            {
                                //class 
                                int clsid = int.Parse(obj[0].Trim());
                                int attid = int.Parse(obj[1].Trim());
                                //find class
                                ClassModel clsdal = GetClassByClassId(clsid);

                                //syntaxDB = string.Concat(resultClass.Name, ".", resultAtt.Name); //$('#NumberToOrder').val()
                                string attname = clsdal.GetAttributes().Where(m => m.Id == attid).Select(x => x.Name).First();
                                syntaxDB = string.Concat("$('#", clsdal.GetAttributes().Where(m => m.Id == attid).Select(x => x.Name).First(), "').val()");
                            }
                            else if (obj.Count() == 1)
                            {
                                switch (obj[0].Trim())
                                {
                                    case "s": syntaxDB = string.Concat("'", " ", "'"); break;
                                    case "c": syntaxDB = string.Concat("'", ",", "'"); break;
                                    default: syntaxDB = ""; break;
                                }
                            }

                        }
                        else
                        {
                            syntaxDB = newWord;
                        }

                        //add Dictionary
                        if (!temp.ContainsKey(newWord))
                        {
                            temp.Add(newWord, syntaxDB);
                        }
                    }
                }
            }
            else //if (input.DataType.Name.Equals("decimal"))   
            {
                //add result dictionary to main dictionary
                
                foreach (var dicValue in MathFunction(input.Formula))
                    temp.Add(dicValue.Key, dicValue.Value);
                //temp = MathFunction(input.Formula);

            }


            foreach (var a in temp.OrderByDescending(m => m.Key.Length))
            {
                oldFormula = oldFormula.Replace(a.Key, a.Value);
            }

            string designFormula = oldFormula;
            return designFormula;
        }
        
        private Dictionary<string, string> MathFunction(string inputSentence)
        {
            Dictionary<string, string> temp = new Dictionary<string, string>();
            string[] separators = new string[] { "+", "-", "*", "/", "^", "(", ")", " ", "Minimum", "Maximun", "Summary", "[", "]", "DateDiff", "," };
            string[] words = inputSentence.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            foreach (var word in words)
            {
                //Console.WriteLine(word);
                string syntaxDB = "";
                if (word.IndexOf('$') == 0)
                {
                    string[] split = { "$", ".", " " };
                    string[] obj = word.Split(split, StringSplitOptions.RemoveEmptyEntries);

                    //class 
                    int clsid = int.Parse(obj[0].Trim());
                    int attid = int.Parse(obj[1].Trim());
                    //find class
                    ClassModel clsdal = GetClassByClassId(clsid);

                    //syntaxDB = string.Concat(resultClass.Name, ".", resultAtt.Name); //$('#NumberToOrder').val()
                    string attname = clsdal.GetAttributes().Where(m => m.Id == attid).Select(x => x.Name).First();
                    AttributeModel attdal = clsdal.GetAttributes().Where(m => m.Id == attid).First();
                    if (attdal.DataType.Name.Equals("DateTime"))
                    {
                        syntaxDB = string.Concat("$('#", clsdal.GetAttributes().Where(m => m.Id == attid).Select(x => x.Name).First(), "').val()");
                    }
                    else
                    {
                        syntaxDB = string.Concat("parseFloat($('#", clsdal.GetAttributes().Where(m => m.Id == attid).Select(x => x.Name).First(), "').val().replace(',', ''))");
                    }
                }
                else
                {
                    float a;
                    if (float.TryParse(word, out a))
                    {
                        syntaxDB = "parseFloat(" + word + ")";
                    }

                }

                //add Dictionary
                if (!temp.ContainsKey(word))
                {
                    temp.Add(word, syntaxDB);
                }
            }
            return temp;
        }

    }
}
